package com.hmy.gateway.controller;

import com.hmy.gateway.bo.request.RefreshTokenRequest;
import com.hmy.gateway.bo.request.ReqUserLogin;
import com.hmy.gateway.service.OAuth2AuthenticationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.rmi.ServerException;

@Api(tags = "用户鉴权接口")
@RestController
@Slf4j
@RequestMapping("/auth")
@Validated
@AllArgsConstructor
public class AuthController {

    private final OAuth2AuthenticationService authenticationService;

    /**
     * 登录
     *
     * @param reqUserLogin ReqLogin
     * @return token及用户信息
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public OAuth2AccessToken authenticate(@RequestBody @Validated ReqUserLogin reqUserLogin) throws ServerException {
        return authenticationService.authenticate(reqUserLogin);
    }

    /**
     * 刷新token
     *
     * @param  refreshTokenRequest
     * @return 用户信息
     */
    @ApiOperation("刷新token")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "刷新token")
    })
    @PostMapping("/refresh-token")
    public OAuth2AccessToken refreshToken(@RequestBody @Validated RefreshTokenRequest refreshTokenRequest) {
        log.info("token:{}",refreshTokenRequest.toString());
        return authenticationService.authenticate(refreshTokenRequest.getToken());
    }

}
